package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.Activity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 活动Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface ActivityMapper extends BaseMapper<Activity> {

    /**
     * 获取最新活动（按创建时间排序）
     */
    @Select("SELECT * FROM activity WHERE expired = 0 ORDER BY createdAt DESC LIMIT #{limit}")
    List<Activity> getActiveByNew(@Param("limit") Integer limit);

    /**
     * 获取最新活动Web版（分页）
     */
    @Select("SELECT * FROM activity WHERE expired = 0 ORDER BY createdAt DESC LIMIT #{offset}, #{size}")
    List<Activity> getActiveByNewWeb(@Param("offset") Integer offset, @Param("size") Integer size);

    /**
     * 获取活动总数（未过期）
     */
    @Select("SELECT COUNT(*) FROM activity WHERE expired = 0")
    Integer getActiveCount();

    /**
     * 根据ID获取活动详情
     */
    @Select("SELECT * FROM activity WHERE activityid = #{activityid}")
    Activity getActivityById(@Param("activityid") Integer activityid);

    /**
     * 分页获取未过期活动
     */
    @Select("SELECT * FROM activity WHERE expired = 0 ORDER BY createdAt DESC LIMIT #{offset}, #{size}")
    List<Activity> getActiveByPageIng(@Param("offset") Integer offset, @Param("size") Integer size);

    /**
     * 分页获取已过期活动
     */
    @Select("SELECT * FROM activity WHERE expired = 1 ORDER BY createdAt DESC LIMIT #{offset}, #{size}")
    List<Activity> getActiveByPageEd(@Param("offset") Integer offset, @Param("size") Integer size);

    /**
     * 获取已过期活动总数
     */
    @Select("SELECT COUNT(*) FROM activity WHERE expired = 1")
    Integer getExpiredActiveCount();

    /**
     * 按月份获取活动（指定月份）
     */
    @Select("SELECT * FROM activity WHERE DATE_FORMAT(STR_TO_DATE(createdAt, '%Y-%m-%d %H:%i:%s'), '%Y-%m') = #{month} " +
            "ORDER BY createdAt DESC LIMIT #{offset}, #{size}")
    List<Activity> getActiveByMonth(@Param("month") String month, @Param("offset") Integer offset, @Param("size") Integer size);

    /**
     * 按月份获取活动总数
     */
    @Select("SELECT COUNT(*) FROM activity WHERE DATE_FORMAT(STR_TO_DATE(createdAt, '%Y-%m-%d %H:%i:%s'), '%Y-%m') = #{month}")
    Integer getActiveCountByMonth(@Param("month") String month);

    /**
     * 按月份获取该月份内全部活动
     */
    @Select("SELECT * FROM activity WHERE DATE_FORMAT(STR_TO_DATE(createdAt, '%Y-%m-%d %H:%i:%s'), '%Y-%m') = #{month} " +
            "ORDER BY createdAt DESC")
    List<Activity> getActiveByMonthAll(@Param("month") String month);

    /**
     * 根据活动大类分页获取活动
     */
    @Select("SELECT * FROM activity WHERE bigid = #{bigid} ORDER BY createdAt DESC LIMIT #{offset}, #{size}")
    List<Activity> getActivedByPage(@Param("bigid") Integer bigid, @Param("offset") Integer offset, @Param("size") Integer size);

    /**
     * 根据活动大类获取活动总数
     */
    @Select("SELECT COUNT(*) FROM activity WHERE bigid = #{bigid}")
    Integer getActivedCountByBigId(@Param("bigid") Integer bigid);
} 